/**
 * 状态管理器 - 用户管理
 **/
 import api from '../../common/request/request'
 
export default {
	namespaced: true,
	state: {
		id: 0, // 用户ID
		user_id: 0, // 用户ID
		isLogin: false, // 登录状态
		username: '', // 用户名
		nickname: '', // 昵称
		mobile: '', // 手机号
		avatar: '', // 默认头像
		gender: 0, // 性别
		birthday: '', // 生日
		longitude: 0, // 经度
		latitude: 0, // 纬度
		token: '', // 令牌
		supportMode: [], // 当前设备支持的生物验证方式
		bio_fingerPrint: false, // 是否已录入指纹识别
		bio_facial: false, // 是否已录入人脸识别
		pushs: true, // 推送
		shock: true, // 震动
		voice: true // 提示音
	},
	mutations: {
		setUserInfo(state, payload) {
			for (let i in payload) {
				for (let j in state) {
					if (i === j) {
						state[j] = payload[i];
					}
				}
			}
		}
	},
	actions: {
		async getUserInfo({state, commit, dispatch, rootState}) {
			api.get({
				url: '/user/userInfo',
				data: {},
				success: res => {
					commit('setUserInfo', res)
					state.isLogin = true // 登录状态强制 开启
					state.pushs = true // 推送
					state.voice = true // 提示音
					state.shock = true 
				},
				fail: err => {}
			})
		},
		async login({state, commit, dispatch, rootState}, data) {
			commit('setUserInfo', data.userinfo)
			state.isLogin = true // 登录状态强制 开启
			state.pushs = true // 推送
			state.voice = true // 提示音
			state.shock = true // 震动
		},
		// 设置经纬度
		// async setLat({state, commit, dispatch, rootState}, data) {
		// 	state.longitude = data.longitude
		// 	state.latitude = data.latitude
		// },
		// 设置生物验证信息
		async setBio({state, commit, dispatch, rootState}, data) {
			commit('setUserInfo', data)
		},
		// async reduceScore({state, commit, dispatch, rootState}, num) {
		// 	let curScore = state.score
		// 	state.score = curScore - num
		// },
		// async addScore({state, commit, dispatch, rootState}, num) {
		// 	let curScore = state.score
		// 	state.score = curScore + num
		// },
		async logout({state, rootState}) {
			for (let j in state) {
				state[j] = ''
			}
			state.isLogin = false;
			let statistics = rootState.statistics;
			for (let j in statistics) {
				for (let i in statistics[j]) {
					statistics[j][i] = 0
				}
			}
			// 从本地缓存中异步移除指定key
			uni.removeStorageSync('wanlshop:user');
			uni.removeStorageSync('wanlshop:statis');
			// 关闭即时通讯
			uni.closeSocket();
		},
		async statistics({state, dispatch, rootState}, payload){
			let states = rootState.statistics;
			for (let i in payload) {
				for (let j in states) {
					if (i === j) {
						states[j] = payload[i];
					}
				}
			}
			uni.setStorageSync("wanlshop:statis", states);
		}
		
	}
};